import Vue from 'vue'
import Router from 'vue-router'

// hack router push callback
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)
const constantRouterMap = [
  {
    path: '/',
    component: () => import('@/views/layouts/index'),
    redirect: '/recommend',
    meta: {
      title: '大师推荐',
      keepAlive: false
    },
    children: [
      {
        path: '/recommend',
        name: 'Recommend',
        component: () => import(/* webpackChunkName: "recommend" */ '@/views/recommend/index'),
        meta: { title: '大师推荐', keepAlive: false }
      },
      {
        path: '/manual',
        name: 'Manual',
        component: () => import(/* webpackChunkName: "manual" */ '@/views/manual/index'),
        meta: { title: '装机宝典', keepAlive: false }
      },
      {
        path: '/self',
        name: 'Self',
        component: () => import(/* webpackChunkName: "manual" */ '@/views/self/index'),
        meta: { title: '自助装机', keepAlive: false }
      },
      {
        path: '/shopping',
        name: 'Shopping',
        component: () => import(/* webpackChunkName: "shopping" */ '@/views/shopping/index'),
        meta: { title: '购物车', keepAlive: false }
      },
      {
        path: '/my',
        name: 'My',
        component: () => import(/* webpackChunkName: "my" */ '@/views/my/index'),
        meta: { title: '我的装机', keepAlive: false }
      }
    ]
  }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // 如果你是 history模式 需要配置vue.config.js publicPath
    // base: process.env.BASE_URL,
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRouterMap
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
